<?php
class Frmcreator_Api_Core extends Core_Api_Abstract
{
    protected $_mappedElements = array(
        'title' => 'title',                    
        'paragraph' => 'description',
        'email' => 'text',
        'signlelinetext' => 'text',
        'multilinetext' => 'textarea',
        'checkbox' => 'checkbox',
        'radio' => 'radio',
        'dropdown' => 'select',
        'multidropdow' => 'multiselect',
        'upload' => 'file',
        'image' => 'text',
        'date' => 'date',
        'captcha' => 'captcha',
        'submit' => 'button',
    ); 
    
    /**
     * Get supported validators
     */
    public function getDefaultValidators(){
        
    }
    
    /**
     * List Forms
     */
    public function getSelectForms(){        
        $ret = array('0' => 'Do not replace');        
        $table = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $params = array();
        $params['status'] = Frmcreator_Model_Form::ACTIVE;
        $select = $table->getFormsSelect($params);        
        $forms = $table->fetchAll($select);        
        foreach($forms as $form){
            $ret[$form->form_id] = $form->title;
        }        
        return $ret;        
    }
    
    /**
     * Return the Zend_Element name mapped to selected element
     */
    public function getMapElement($type){
        if(empty($type)){
            return 'text';
        }
        
        return $this->_mappedElements[$type];
    }
    
    public function getFormFooterMenu($params, $allowInsert = false){        
        if(empty($params['name'])){
            return null;
        }
        
        $table = Engine_Api::_()->getDbtable('menuItems', 'core');
        $select = $table->select();
        $select->where("name = ?", $params['name']);
        
        $menu = $table->fetchRow($select);        
        if($allowInsert && !$menu){
            $menu = $table->createRow();            
        }
        if($menu){
            $menu->setFromArray($params);
            $menu->save();    
        }
        
        return $menu;
    }
    
    public function renderEmailContent($params){
        $ret = "";
        
        foreach($params as $key => $value){
             $parts = explode('_', $key);
             if(count($parts) != 2){
                continue;
             }
             
             list($type, $element_id) = $parts;
             
             $element = Engine_Api::_()->getItem('frmcreator_element', $element_id);
             if($type == "element" && $element){
                if($element->isMultiOptionsElement()){
                    if(is_array($value)){
                        $tmp = array();
                        foreach($value as $option_id){
                            $option = Engine_Api::_()->getItem('frmcreator_option', $option_id);
                            if($option){
                               $tmp[] = $option->label; 
                            }
                        }
                        if(count($tmp) > 0){
                             $ret .= $element->label . ": " .  implode(',', $tmp) . "\r\n";
                        }
                        
                    } else {
                        $option = Engine_Api::_()->getItem('frmcreator_option', $value);
                        if($option){
                              $ret .= $element->label . ": " .  $option->label . "\r\n"; 
                        }    
                    }
                    
                } else {
                    //Normal Element
                    $ret .= $element->label . ": " .  $value . "\r\n";    
                }
                  
             }
        }
        
        return $ret;
    }
}
